package vn.edu.hcmut.cse.ai.search.test;

import java.io.File;
import java.util.Scanner;
import vn.edu.hcmut.cse.ai.search.problems.npuzzle.*;

public class NPuzzle {

    public static void main(String[] args) {
        String pathIn = "./NPuzzle.";
        File folder = new File(pathIn);
        File[] listIn = folder.listFiles();
        long start, end;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Chose " + "1. DFS" + "\t" + "2. BRFS" + "\t" + "3. First Heuristic" + "\t" + "4. Second Heuristic");
        int choice = scanner.nextInt();
        for (int i = 0; i < 10; i++) { // run 10 testcase
            ReadInput read = new ReadInput();
            read.handle(listIn[i].getPath());
            int size = read.getSize();
            int[][] board = new int[size][size];
            int[][] goalBoard = new int[size][size];
            board = read.getStart();
            goalBoard = read.getGoal();
            // Prepare problem
            // start state
            NPuzzleState nPuzzleState = new NPuzzleState(size, board);
            // goal state
            NPuzzleState goalState = new NPuzzleState(size, goalBoard);
            NPuzzleActions nPuzzleActions = new NPuzzleActions();
            NPuzzleGoalTest nPuzzleGoalTest = new NPuzzleGoalTest(goalState);
            NPuzzleActionCost nPuzzleActionCost = new NPuzzleActionCost();
            NPuzzleProblem nPuzzleProblem = new NPuzzleProblem(nPuzzleState, nPuzzleActions, nPuzzleGoalTest, nPuzzleActionCost);
            //BRFS
            threadBFS brfs = new threadBFS(nPuzzleProblem);
            //DFS
            threadDFS dfs = new threadDFS(nPuzzleProblem);
            //first heuristic
            firstHeuristicFunction firstHeuristic = new firstHeuristicFunction(new NPuzzleFirstHeuristicFunction(), nPuzzleProblem);
            //second heuristic
            secondHeuristicFunction secondHeuristic = new secondHeuristicFunction(new NPuzzleSecondHeuristicFunction(), nPuzzleProblem);

            System.out.println("Input" + (i + 1) + ':' + '\n');
            System.out.println("Start State: ");
            for (int i0 = 0; i0 < read.getSize(); i0++) {
                for (int j0 = 0; j0 < read.getSize(); j0++) {
                    System.out.print(read.getStart()[i0][j0]);
                    System.out.print(" ");
                }
                System.out.println("");
            }
            System.out.println("Goal State: ");
            for (int i0 = 0; i0 < read.getSize(); i0++) {
                for (int j0 = 0; j0 < read.getSize(); j0++) {
                    System.out.print(read.getGoal()[i0][j0]);
                    System.out.print(" ");
                }
                System.out.println("");
            }
            System.out.println("Solution: ");
            start = System.currentTimeMillis();
            switch (choice) {
                case 1:
                    dfs.run();
                    break;
                case 2:
                    brfs.run();
                    break;
                case 3:
                    firstHeuristic.run();
                    break;
                case 4:
                    secondHeuristic.run();
                    break;
                default:
                    break;
            }
            end = System.currentTimeMillis();
            System.out.println(end - start + " millis second");
            System.out.println("------------------------------");
        }
    }
}
